//
//  SISSHouseConditionInfoListViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/9/15.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSHouseConditionInfoListViewController.h"
#import "SISSHouseConditionInfoListCell.h"
#import "SISSHouseConditionInfoViewController.h"
#import "SISSUploadHouseConditionInfoViewController.h"
#import "SISSNetworkingManager+HouseProgress.h"
#import "SISSHouseProgressModel.h"
#import "SISSHouseConditionInfoModel.h"
#import "SISSNetworkingManager+RentOutProgress.h"
#import "SISSRentOutProgressModel.h"

static NSString *houseConditionInfoListCellReuseID = @"houseConditionInfoListCellReuseID";
@interface SISSHouseConditionInfoListViewController ()<UITableViewDelegate,UITableViewDataSource>

/// tableView
@property (weak, nonatomic) SISSBaseTableView *tableView;
/// updateConditionInfoButton
@property (weak, nonatomic) UIButton *updateConditionInfoButton;

/// houseConditionInfoArr
@property (copy, nonatomic) NSArray<SISSHouseConditionInfoModel *> *houseConditionInfoArr;

@end

@implementation SISSHouseConditionInfoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"房屋状况说明书";

    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf requestHouseConditionInfoList];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self requestHouseConditionInfoList];
}

#pragma mark - 点击更新房屋状况说明书按钮
- (void)updateConditionInfoButtonDidClickAction:(UIButton *)button {
//    if (self.houseProgressRecordInfoModel.businessStatus == 2) {
//        [self alertActionAlertWithTitle:@"提示" message:@"对方已开启拒绝接收您的请求"];
//        return;
//    }
    NSLog(@"更新房屋状况说明书");
    SISSUploadHouseConditionInfoViewController *vc = [[SISSUploadHouseConditionInfoViewController alloc] init];
//    vc.houseProgressModel = self.houseProgressModel;
//    vc.rentOutProgressModel = self.rentOutProgressModel;
    vc.houseProgressModel = self.houseProgressModel;
    vc.rentOutProgressModel = self.rentOutProgressModel;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.houseConditionInfoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSHouseConditionInfoListCell *cell = [tableView dequeueReusableCellWithIdentifier:houseConditionInfoListCellReuseID forIndexPath:indexPath];
    cell.houseConditionInfoModel = self.houseConditionInfoArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SISSHouseConditionInfoViewController *vc = [[SISSHouseConditionInfoViewController alloc] init];
    vc.houseProgressModel = self.houseProgressModel;
    vc.rentOutProgressModel = self.rentOutProgressModel;
    vc.ID = self.houseConditionInfoArr[indexPath.row].ID;
    vc.isHistory = self.isHistory ? self.isHistory : ![[SISSUserAccount shared].spId isEqualToString:self.houseConditionInfoArr[indexPath.row].spId];
    [self.navigationController pushViewController:vc animated:YES];

}

#pragma mark - 加载数组
- (void)requestHouseConditionInfoList {
    
    WeakSelf
    
    if (self.houseProgressModel) {
        [kNetworkManager requestHouseProgressHouseStatusManualListAhrId:self.houseProgressModel.house.ID success:^(NSArray<SISSHouseConditionInfoModel *> *houseConditionInfoArr) {
            
            weakSelf.houseConditionInfoArr = houseConditionInfoArr;
//            for (SISSHouseConditionInfoModel *model in houseConditionInfoArr) {
//                if (model.sellerState == 0) {
//                    weakSelf.updateConditionInfoButton.userInteractionEnabled = NO;
//                    [weakSelf.updateConditionInfoButton setBackgroundColor:SECONDARY_TEXT_COLOR];
//                    break;
//                } else {
//                    weakSelf.updateConditionInfoButton.userInteractionEnabled = YES;
//                    [weakSelf.updateConditionInfoButton setBackgroundColor:MAIN_COLOR];
//                }
//            }
            CGFloat tableViewHeight = 10 * globelMargin * houseConditionInfoArr.count;
            if (tableViewHeight > SCREEN_HEIGHT - 56 - kTopHeight) {
                tableViewHeight = SCREEN_HEIGHT - 56 - kTopHeight;
                [self.updateConditionInfoButton mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(weakSelf.tableView.mas_bottom).offset(globelMargin);
                }];
            }
            [weakSelf.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(tableViewHeight);
            }];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
            }];
            
        } failed:^(NSError *error) {
            [weakSelf.tableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    } else {
        [kNetworkManager requestRentOutProgressHouseStatusManualListAlrId:self.rentOutProgressModel.lease.ID success:^(NSArray<SISSHouseConditionInfoModel *> *houseConditionInfoArr) {
            
            weakSelf.houseConditionInfoArr = houseConditionInfoArr;
//            for (SISSHouseConditionInfoModel *model in houseConditionInfoArr) {
//                if (model.sellerState == 0) {
//                    weakSelf.updateConditionInfoButton.userInteractionEnabled = NO;
//                    [weakSelf.updateConditionInfoButton setBackgroundColor:SECONDARY_TEXT_COLOR];
//                    break;
//                } else {
//                    weakSelf.updateConditionInfoButton.userInteractionEnabled = YES;
//                    [weakSelf.updateConditionInfoButton setBackgroundColor:MAIN_COLOR];
//                }
//            }
            [weakSelf.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(10 * globelMargin * houseConditionInfoArr.count);
            }];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
            }];
            
        } failed:^(NSError *error) {
            [weakSelf.tableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
}


#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    // 自动行高
    tableView.estimatedRowHeight = 80;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerClass:[SISSHouseConditionInfoListCell class] forCellReuseIdentifier:houseConditionInfoListCellReuseID];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    // 添加看房记录按钮(只有合作方才能添加带看)
    UIButton *updateConditionInfoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [updateConditionInfoButton setTitle:@"创建新版房屋状况说明书" forState:UIControlStateNormal];
    [updateConditionInfoButton setBackgroundColor:MAIN_COLOR];
    [updateConditionInfoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    updateConditionInfoButton.hidden = self.type == 1; // 发布方隐藏
//    if (self.isHistory) {
//        updateConditionInfoButton.hidden = YES;
//    }
    updateConditionInfoButton.hidden = self.isHistory;
    [updateConditionInfoButton addTarget:self action:@selector(updateConditionInfoButtonDidClickAction:) forControlEvents:UIControlEventTouchUpInside];
    updateConditionInfoButton.layer.cornerRadius = 4;
    updateConditionInfoButton.layer.masksToBounds = YES;
    [self.view addSubview:updateConditionInfoButton];
    self.updateConditionInfoButton = updateConditionInfoButton;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    // 自动布局
    WeakSelf
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_topLayoutGuideBottom);
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(64);
    }];
    
    [self.updateConditionInfoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH * 0.8, 44));
        make.top.equalTo(weakSelf.tableView.mas_bottom).offset(4 * globelMargin);
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
